﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVCTippSpielManagement.Utilities;
using System.Data.SqlClient;
using System.Data;
using MVCTippSpielManagement.Management.Structs;

namespace MVCTippSpielManagement.Management
{
	public class UserManagement
	{
		/// <summary>
		/// Überprüft Benutzername und Passwort mit den Werten
		/// aus der Datenbank
		/// </summary>
		/// <param name="username">Benutzername</param>
		/// <param name="passwort">Passwort</param>
		/// <returns>True bei Erfolg sonst False</returns>
		public bool VerifyLoginData(string username, string passwort)
		{
			MyApplication _Application = MyApplication.Instance;

			bool VerifyOK;
			VerifyOK = false;

			if (_Application._LogOnUser.Username.Trim() == username.Trim() && _Application._LogOnUser.Password.Trim() ==  Util.GetMD5Hash(passwort.Trim()))
			{
				VerifyOK = true;
			}

			return VerifyOK;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public bool Logout()
		{

			return true;
		}

		/// <summary>
		/// Ändert Passwort 
		/// </summary>
		/// <returns></returns>
		public bool ChangePassword()
		{
			return true;
		}


		public Dictionary<string, Address> GetUserAddress(int userID)
		{

			Dictionary<string, Address> _Address;
			_Address = new Dictionary<string, Address>();
			

			Address _AddressItem;
			_AddressItem = new Address();

			try
			{
				SqlParameter _Parameter = new SqlParameter("@cID", userID);
				DataTable dt = DataBaseManager.ExecuteDataTable("GETUserAdresses", _Parameter);

				foreach (DataRow dr in dt.Rows)
				{
					_AddressItem = new Address();

					_AddressItem.ZipCode = dr["Postleitzahl"].ToString().Trim();
					_AddressItem.City = dr["Ort"].ToString().Trim();
					_AddressItem.Street = dr["Strasse"].ToString().Trim();
					_AddressItem.Number = dr["Hausnummer"].ToString().Trim();

					_Address.Add(dr["FK_AdressArt"].ToString().Trim(), _AddressItem);
				}

			}
			catch (Exception)
			{
				// ErrorMessage 
			}
			return _Address;
		}


		/// <summary>
		/// 
		/// </summary>
		/// <param name="userID"></param>
		/// <returns></returns>
		public Dictionary<int, string> GetUserGroups(int userID)
		{

			Dictionary<int, string> _UserGroups;

			int _Benutzergruppe;
			string _Bezeichnung;

			// Create a object 
			_UserGroups = new Dictionary<int, string>();

			try
			{

				SqlParameter _Parameter = new SqlParameter("@cID", userID);
				DataTable dt = DataBaseManager.ExecuteDataTable("GETUserUsergroups", _Parameter);

				foreach (DataRow dr in dt.Rows)
				{
					_Benutzergruppe = System.Convert.ToInt32(dr["FK_Benutzergruppe"].ToString().Trim());
					_Bezeichnung = dr["Bezeichnung"].ToString().Trim();

					_UserGroups.Add(_Benutzergruppe, _Bezeichnung);
				}

			}
			catch (Exception)
			{
				// ErrorMessage 
			}

			return _UserGroups;
		}


		/// <summary>
		/// 
		/// </summary>
		/// <param name="userID"></param>
		/// <param name="expected"></param>
		/// <returns></returns>
		public bool UserHasState(int userID, string expected)
		{
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="userID"></param>
		/// <param name="state"></param>
		/// <returns></returns>
		public bool SetUserState(int userID, string state)
		{
			return true;
		}

		/// <summary>
		/// Generiert ein Passwort
		/// </summary>
		/// <returns></returns>
		public string CreatePassword()
		{

			return "";
		}

		/// <summary>
		/// Fensterzustände (Maximiert, Position)
		/// </summary>
		/// <returns></returns>
		public bool SaveUserXMLConfig()
		{
			return true;
		}

		/// <summary>
		/// Fensterzustände reproduzieren
		/// </summary>
		/// <returns></returns>
		public bool LoadUserXMLConfig()
		{
			return true;
		}

	}
}
